package com.sky.happygos.present;

import android.util.Log;

import com.sky.happygos.event.CreateGoodsE;
import com.sky.happygos.event.CreateShopE;
import com.sky.happygos.event.ShopE;
import com.sky.happygos.event.UpdateShopE;
import com.sky.happygos.model.Goods;
import com.sky.happygos.model.Shop;
import com.sky.happygos.network.Api;

import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EBean;
import org.greenrobot.eventbus.EventBus;

import java.io.File;
import java.util.List;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static org.androidannotations.annotations.EBean.Scope.Singleton;

/**
 * Created by qqqqqqq on 2017/4/22.
 */
@EBean(scope = Singleton)
public class ShopP {

    @Bean
    Api api;

    @Background
    public void updateShop(String path,Shop shop){

        MultipartBody.Builder builder = new MultipartBody.Builder()
                .setType(MultipartBody.FORM);
        builder.addFormDataPart("id",shop.id);
        builder.addFormDataPart("name",shop.name);
        builder.addFormDataPart("info",shop.info);
        builder.addFormDataPart("school",shop.school);
        builder.addFormDataPart("addr",shop.addr+"");

        if(path != null) {
            File file = new File(path);
            RequestBody imageBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
            builder.addFormDataPart("icon", file.getName(), imageBody);
        }

        List<MultipartBody.Part> parts = builder.build().parts();

        Call<Shop> call = api.getAuthService().updateShop(parts);
        call.enqueue(new Callback<Shop>() {
            @Override
            public void onResponse(Call<Shop> call, Response<Shop> response) {

                if(response.code() == 200) {
                    Shop g = response.body();
                    EventBus.getDefault().post(new UpdateShopE(g,true));
                }else{
                    EventBus.getDefault().post(new UpdateShopE(response));
                }
            }

            @Override
            public void onFailure(Call<Shop> call, Throwable t) {
                EventBus.getDefault().post(new UpdateShopE(null));
            }
        });
    }

    @Background
    public void getMyShop(){
        Call<List<Shop>> call = api.getAuthService().myShop();
        call.enqueue(new Callback<List<Shop>>() {
            @Override
            public void onResponse(Call<List<Shop>> call, Response<List<Shop>> response) {
                if(response.code() == 200){
                    List<Shop> shops = response.body();
                    EventBus.getDefault().post(new ShopE(shops,true));
                }else{
                    EventBus.getDefault().post(new ShopE(response));
                }
                Log.v("shop:===>",response.code()+"");
            }

            @Override
            public void onFailure(Call<List<Shop>> call, Throwable t) {
                EventBus.getDefault().post(new ShopE(null));
            }
        });
    }


    @Background
    public void createShop(String path,Shop shop){

        MultipartBody.Builder builder = new MultipartBody.Builder()
                .setType(MultipartBody.FORM);
        builder.addFormDataPart("name",shop.name);
        builder.addFormDataPart("info",shop.info);
        builder.addFormDataPart("school",shop.school);
        builder.addFormDataPart("addr",shop.addr+"");

        if(path != null) {
            File file = new File(path);
            RequestBody imageBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
            builder.addFormDataPart("icon", file.getName(), imageBody);
        }

        List<MultipartBody.Part> parts = builder.build().parts();

        Call<Shop> call = api.getAuthService().createShop(parts);
        call.enqueue(new Callback<Shop>() {
            @Override
            public void onResponse(Call<Shop> call, Response<Shop> response) {

                if(response.code() == 200) {
                    Shop g = response.body();
                    EventBus.getDefault().post(new CreateShopE(g,true));
                }else{
                    EventBus.getDefault().post(new CreateShopE(response));
                }
            }

            @Override
            public void onFailure(Call<Shop> call, Throwable t) {
                EventBus.getDefault().post(new CreateShopE(null));
            }
        });
    }
}
